<?php
namespace app\common\model;
use app\common\cache\CacheData;

class CoinConfig extends Base
{
	static $config_type = [
                'charge'=>'charge',
                'withdraw'=>'withdraw',
                'transfer'=>'transfer',
                'convert'=>'convert'
            ];

	public function cacheData(){
		$data = $this->select();
		if($data){
			foreach($data as &$value):
				$value=$value->toArray();
			endforeach;
		}
		return $data;
	}

	/**
     * 货币配置缓存
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function coinConfigData($id=null){
        $CACHE = CacheData::getInstance('CoinConfig');
        $cacheData = $CACHE -> readCache();
        $data = [];
        foreach($cacheData as $cache):
            $data[$cache['id']] = $cache;
        endforeach;
        if(!is_null($id)){
            if(isset($data[$id])):
                return $data[$id];
            else:
                return false;
            endif;
        }else{
            return $data;
        }
    }


     /**
     * 货币配置by Type
     * @param  [type] $type [description]
     * @return [type]       [description]
     */
    public static function coinConfigByType($type=null){        
        // 所有货币缓存数据
        $coinData = Coin::coinData();

        // 所有货币配置缓存数据
        $configCache = self::coinConfigData();
        $configData = [];
        // 处理缓存数据
        foreach($configCache as $cache):
            $cache['coin_name'] = $coinData[$cache['coin_id']]['coin_name'];
            $cache['convert_coin_name'] = isset($coinData[$cache['convert_coin_id']])?$coinData[$cache['convert_coin_id']]['coin_name']:'';
            $configData[$cache['id']] = $cache;
        endforeach;

        if(is_null($type))
            return $configData;

        $data = [];
        foreach($configData as $value):
            if($value['type']==$type)
                $data[] = $value;
        endforeach;
        return collection($data)->toArray();
    }

    /**
     * 货币转换配置处理
     * @return [type] [description]
     */
    public static function convertCoinConfig(){
        $configData = self::coinConfigByType('convert');
        $data = [];
        foreach($configData as $value):
            $data[$value['coin_id']]['coin_id']=$value['coin_id'];
            $data[$value['coin_id']]['coin_name']=$value['coin_name'];
            $data[$value['coin_id']]['target'][]=$value;
        endforeach;
        return $data;
    }

    public function saveOneData($data){
        $model = new CoinConfigModel;
        return $model -> save($data);
    }

    public function addOneData($data,$type){
        if(false==$this->isExist($data,$type)){
            $this->save($data);
        }else{
            $this->error='配置项已经存在';
            return false;
        }
    }

    public function isExist($data,$type){
        if($type==self::$config_type['convert']):
            if($this->where(['coin_id'=>$data['coin_id'],'convert_coin_id'=>$data['convert_coin_id'],'type'=>$type])->find())
                return true;
            else
                return false;
        else:
            if($this->where(['coin_id'=>$data['coin_id'],'type'=>$type])->find())
                return true;
            else
                return false;
        endif;
    }

}
